
let prefabCache = {};
/**
     * 加载预制体
     * @param {string} sceneName 场景名称
     * @param {string} prefabPath 预制体路径
     * @param {function} onProgress 加载进度回调 (0-1)
     * @param {function} onComplete 加载完成回调
     */
const loadPrefab = function (prefabPath, onProgress, onComplete) {
    // 使用 cc.resources.load
    // console.log(cc.resources)
    cc.resources.load(
        prefabPath,
        cc.Prefab,
        function (finished, total, item) {
    // console.log(cc.resources)
            // 计算进度
            // var progress = finished / total;
            // if (onProgress) {
            //     onProgress(progress);
            // }
        },
        function (error, prefab) {
    console.log(cc.resources)
            // if (error) {
            //     cc.error("加载预制体 " + prefabPath + " 失败:", error);
            //     return;
            // }

            // if (onComplete) {
            //     onComplete(prefab);
            // }
        }
    );
}


/**
 * 批量加载预制体
 * @param {string} sceneName 场景名称
 * @param {string[]} prefabPaths 预制体路径数组
 * @param {function} onSingleProgress 单个预制体加载进度
 * @param {function} onAllProgress 所有预制体总体进度
 * @param {function} onSingleComplete 单个预制体加载完成
 * @param {function} onAllComplete 所有预制体加载完成
 */
const loadPrefabs = function (sceneName, prefabPaths, onSingleProgress, onAllProgress, onSingleComplete, onAllComplete) {
    var total = prefabPaths.length;
    var loadedCount = 0;
    var allPrefabs = new Array(total);
    var self = this;

    prefabPaths.forEach(function (path, index) {
        self.loadPrefab(
            sceneName,
            path,
            function (progress) {
                if (onSingleProgress) {
                    onSingleProgress(index, progress);
                }

                // 计算总体进度
                var overallProgress = (loadedCount + progress) / total;
                if (onAllProgress) {
                    onAllProgress(overallProgress);
                }
            },
            function (prefab) {
                allPrefabs[index] = prefab;
                loadedCount++;

                if (onSingleComplete) {
                    onSingleComplete(index, prefab);
                }

                // 计算总体进度
                if (onAllProgress) {
                    onAllProgress(loadedCount / total);
                }

                // 检查是否全部加载完成
                if (loadedCount === total && onAllComplete) {
                    onAllComplete(allPrefabs);
                }
            }
        );
    });
}

/**
 * 带缓存的加载预制体
 * @param {string} sceneName 场景名称
 * @param {string} prefabPath 预制体路径
 * @param {function} onProgress 加载进度回调
 * @param {function} onComplete 加载完成回调
 */
const loadPrefabWithCache = function (sceneName, prefabPath, onProgress, onComplete) {
    // 检查缓存
    if (prefabCache[prefabPath]) {
        if (onComplete) {
            onComplete(prefabCache[prefabPath]);
        }
        return;
    }
    loadPrefab(sceneName, prefabPath, onProgress, function (prefab) {
        // 存入缓存
        prefabCache[prefabPath] = prefab;
        if (onComplete) {
            onComplete(prefab);
        }
    });
}

/**
 * 释放缓存的预制体
 * @param {string} prefabPath 预制体路径
 */
const releasePrefab = function (prefabPath) {
    if (prefabCache[prefabPath]) {
        cc.resources.release(prefabPath, cc.Prefab);
        delete prefabCache[prefabPath];
    }
}

module.exports = {
    releasePrefab,
    loadPrefab,
    loadPrefabs,
    loadPrefabWithCache
};